<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/page/inc/taglib.jsp"%>
<style>
.buttonRight {
	position: absolute;
	left: 525px;
	bottom: -18.9px;
}
</style>
<div class="pageContent">
	<form method="post" action="${pageContext.request.contextPath}${requestScope.record ==null ?'/warehouse/wareOutOrder/insert' :'/warehouse/wareOutOrder/update' }" class="pageForm required-validate" onsubmit="return validateCallback(this, dialogAjaxDone);">
		<div class="pageFormContent" layoutH="56">
			<!-- 关闭窗口 -->
			<input type="hidden" name="callbackType" value="closeCurrent" />
			<!-- 隐藏表单 -->
			<c:if test="${requestScope.record !=null}">
				<input type="hidden" name="id" value="${requestScope.record.id}" />
			</c:if>
			<fieldset>
				<legend>订货单</legend>
				<p>
					<label>店铺名称：</label>
					<select name="shopId" class="combox">
						<option value="">请选择</option>
						<c:forEach items="${requestScope.shops}" var="item">
						<option value="${item.shopId }">${item.shopName}</option>
						</c:forEach>
					</select>
				</p>
				<p>
					<label>总价：</label><span></span>
				</p>
			</fieldset>
			<fieldset>
				<legend>购物车列表</legend>
				<table>
					<tr>
						<td>商品名称</td>
						<td>商品编号</td>
						<td>数量</td>
						<td>单价</td>	
						<td>总价</td>	
						<td>操作</td>	
					</tr>
					<tr>
						<td>
							<select name="pnid" onchange="changeVal(this)">
								<option value="">请选择</option>
								<c:forEach items="${requestScope.productNorms}" var="norms">
									<option value="${norms.merchantProductNormId}">${norms.normName}</option>
								</c:forEach>
							</select>
						</td>
						<td></td>
						<td></td>
						<td></td>
						<td></td>
						<td></td>
					</tr>
				</table>
			</fieldset>
			
		</div>
		<div class="formBar">
			<ul>
				<!--<li><a class="buttonActive" href="javascript:;"><span>保存</span></a></li>-->
				<li><div class="buttonActive">
						<div class="buttonContent">
							<button type="submit">保存</button>
						</div>
					</div></li>
				<li>
					<div class="button">
						<div class="buttonContent">
							<button type="button" class="close">取消</button>
						</div>
					</div>
				</li>
			</ul>
		</div>
	</form>
</div>
<script type="text/javascript">
	function addParameter() {
		$("#parameter").append("<div class='unit'><input type='hidden' name='wioodid'>" + "<label style='width: 74px;'>商品规格名称</label><select name='pnid' onchange='changeVal(this)'>" + "<option value=''>请选择</option>" + "<c:forEach items='${requestScope.productNorms}' var='norms'>" + "<option value='${norms.merchantProductNormId}'>${norms.normName}</option>" + "</c:forEach>" + "</select>" + "<label style='width: 50px;'>仓库名称</label><select name='whid' onchange='changeVal(this)'>" + "<option value=''>请选择</option>" + "<c:forEach items='${requestScope.warehouses}' var='witems'>" + "<option value='${witems.id}'>${witems.name}</option>" + "</c:forEach>" + "</select><label style='width: 24px;'>数量</label><input class='textInput digits' onchange='changeVal(this)' type='text' name='amut' value=''>" + "</div>");
	}
	function changeVal(e) {
		var pnid = $(e).parent().find("[name='pnid']").val();
		var whid = $(e).parent().find("[name='whid']").val();
		var amut = $(e).parent().find("[name='amut']").val();

		var data = {
			pnid : pnid,
			whid : whid,
			amut : amut
		};
		console.log(data);
		if (data.pnid && data.whid && data.amut) {
			//Ajax向后台提交数据  
			$.ajax({
				//type : form.method || 'POST',
				url : "${pageContext.request.contextPath}/warehouse/wareOutOrder/getWarning",
				//获取表单的内容数据  
				data : data,
				//dataType : "json",
				//cache : false,
				//执行回调函数  
				success : function(result) {
					var code = result.statusCode;
					var msg = result.message;
					if (code == 300) {
						alertMsg.warn(msg);
					}
				},
				error : function(result) {
					alert(result)
				}
			});
		}
	}

	function onumChange(e) {
		if (e.value) {
			var data = {
				onum : e.value
			};
			//Ajax向后台提交数据  
			$.ajax({
				//type : form.method || 'POST',
				url : "${pageContext.request.contextPath}/warehouse/wareOutOrder/onumChange",
				//获取表单的内容数据  
				data : data,
				//dataType : "json",
				//cache : false,
				//执行回调函数  
				success : function(result) {
					console.log(result);
					var code = result.statusCode;
					var msg = result.message;
					if (code == 200) {
						var content = result.content;
						var merchantOrder = content.merchantOrder;
						var orderInfo = content.orderInfo;
						if (merchantOrder) {
							$("[name='opcsPice']").val(merchantOrder.orderMoney / 100);
						}
					
						if (orderInfo) {
							$("#parameter").html("");
							for (var i = 0; i < orderInfo.length; i++) {
								$("#parameter").append(
										"<div class='unit'>                                                                                                                                             "
										+"	<input type='hidden' name='wioodid' value='"+orderInfo[i].mbid+"'> <label style='width: 74px;'>商品规格名称</label> <select id='pnid"+i+"' name='pnid' onchange='changeVal(this)'>    "
										+"		<option value=''>请选择</option>                                                                                                                        "
										+"		<c:forEach items='${requestScope.productNorms}' var='norms'>                                                                                            "
										+"			<option value='${norms.merchantProductNormId}' "+orderInfo[i].mbnid+" a=${norms.merchantProductNormId}>${norms.normName}</option>                 "
										+"		</c:forEach>                                                                                                                                            "
										+"	</select> <label style='width: 50px;'>仓库名称</label> <select name='whid' onchange='changeVal(this)'>                                                      "
										+"		<option value=''>请选择</option>                                                                                                                        "
										+"		<c:forEach items='${requestScope.warehouses}' var='witems'>                                                                                             "
										+"			<option value='${witems.id}'>${witems.name}</option>                                                        "
										+"		</c:forEach>                                                                                                                                            "
										+"	</select> <label style='width: 24px;'>数量</label><input type='text' name='amut' class='textInput digits' value='"+orderInfo[i].mbcount+"' onchange='changeVal(this)'>  "
										+"	<c:if test='${status.index==0 }'>                                                                                                                           "
										+"		<a class='button buttonRight' href='javascript:addParameter();'><span>＋添加</span></a>                                                                 "
										+"	</c:if>                                                                                                                                                     "
										+"</div>                                                                                                                                                         ");
							}
							for (var i = 0; i < orderInfo.length; i++) {
								$("#pnid"+i+" option").each(function(){
									if($(this).val()==orderInfo[i].mbnid){
										$(this).attr("selected","selected");
									}
								});
							}
						}
					}
				},
				error : function(result) {
					alert(result)
				}
			});
		}
	}
</script>